﻿using System.Collections.Generic;

using System.Web;

using System.Linq;

using System;
using ShopB2C.Core.Entities;


namespace ShopB2C.Core.Helper
{
    /// <summary>
    /// 消息类
    /// </summary>
    public class MessageHelper
    {
        private MessageStatus _status;
        private string _title, _body;
        private List<string> _msgCollection;
        private List<string> _operateList;

        /// <summary>
        /// 验证通过返回 true
        /// </summary>
        public bool IsValid
        {
            get { return _msgCollection.Count < 1; }
        }
        /// <summary>
        /// 消息类型
        /// </summary>
        public MessageStatus MessageStatus
        {
            get { return _status; }
        }
        /// <summary>
        /// 构造函数, 传入 MessageType 标志(消息的类型)
        /// </summary>
        /// <param name="msgType">MessageType 标志(消息的类型)</param>
        public MessageHelper(MessageStatus status) : this(status, null) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="messageType">MessageType 标志(消息的类型)</param>
        /// <param name="defaultOperate">默认操作</param>
        public MessageHelper(MessageStatus status, string defaultOperate)
        {
            this._msgCollection = new List<string>();
            this._title = string.Empty;
            this._body = string.Empty;
            this._status = status;
            this._operateList = new List<string>();
            if (string.IsNullOrEmpty(defaultOperate))
            {
                this._operateList.Add(
                    string.Format("<a href=\"javascript:{0};\">{1}</a>",
                    JavaScriptHelper.Alert(null, AlertType.GoBack, "-1"),
                    "确定"));
            }
            else
            {
                this.AddOperate("确定", defaultOperate);
            }
        }
        /// <summary>
        /// 增加消息主体
        /// </summary>
        /// <param name="body">消息主体</param>
        /// <param name="title">消息标题</param>
        public void AddBody(string msgBody, string msgTitle)
        {
            this.AddBody(_status, msgBody, msgTitle);
        }
        /// <summary>
        /// 增加消息主题
        /// </summary>
        /// <param name="msgBody">消息主体，并且将消息标题也设置为消息主体一样的字符串</param>
        public void AddBody(string msgBody)
        {
            this.AddBody(msgBody, msgBody);
        }
        /// <summary>
        /// 增加消息主体
        /// </summary>
        /// <param name="msgStatus">状态枚举</param>
        /// <param name="msgTitle">消息标题</param>
        /// <param name="msgBody">消息主体</param>
        public void AddBody(MessageStatus msgStatus, string msgBody, string msgTitle)
        {
            this._title = msgTitle;
            this._status = msgStatus;
            this._body = msgBody;
        }
        /// <summary>
        /// 增加消息子项
        /// </summary>
        /// <param name="msg">消息子项</param>
        public void AddMessage(string msg)
        {
            this._msgCollection.Add(msg);
        }
        /// <summary>
        /// 创建返回式消息
        /// </summary>
        /// <returns>消息实体</returns>
        public MessageException CreateMessage()
        {
            MessageException msg = new MessageException()
            {
                Title = this._title,
                Body = this._body,
                MessageCollection = this._msgCollection,
                Operate = this._operateList,
                Status = this._status
            };
            return msg;
        }
        /// <summary>
        /// 增加连接地址
        /// </summary>
        /// <param name="text">连接显示的文本</param>
        /// <param name="redirectUrl">连接转到的url</param>
        public void AddOperate(string text, string redirectUrl)
        {
            this._operateList.Add(string.Format("<a href=\"{0}\">{1}</a>", redirectUrl, text));
        }
        /// <summary>
        /// 默认操作
        /// </summary>
        /// <param name="url">Url或者带有javascript:的脚本字符串</param>
        public void DefaultOperate(string url)
        {
            _operateList[0] = string.Format("<a href='{0}'>{1}</a>", url, "确定");
        }

        #region 验证方法

        /// <summary>
        /// 必填
        /// </summary>
        /// <param name="value">验证的值</param>
        /// <param name="msgCode">验证失败的提示信息</param>
        /// <returns>验证成功或失败</returns>
        public bool Required(string value, string msg)
        {
            bool flag = ValidDataHelper.Required(value);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }
        /// <summary>
        /// 指定的最小长度, 亦不能为空
        /// </summary>
        /// <param name="value">验证的值</param>
        /// <param name="length">最小长度</param>
        /// <param name="msgCode">验证失败的提示信息</param>
        /// <returns>验证成功或失败</returns>
        public bool MinLength(string value, int length, string msg)
        {
            bool flag = ValidDataHelper.MinLength(value, length);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }
        /// <summary>
        /// 是否为Email
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="msgCode">验证消息提示错误代码</param>
        /// <returns>验证成功或失败</returns>
        public bool Email(string value, string msg)
        {
            bool flag = ValidDataHelper.IsEmail(value);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }
        /// <summary>
        /// 必须为 int
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="msgCode">验证消息提示错误代码</param>
        /// <param name="result">转换成功后的值</param>
        /// <returns>是否转换成功</returns>
        public bool MustInt(string value, string msg, out int result)
        {
            bool flag = int.TryParse(value, out result);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }
        /// <summary>
        /// 必须为 float
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="msgCode">验证消息提示错误代码</param>
        /// <param name="result">转换成功后的值</param>
        /// <returns>是否转换成功</returns>
        public bool MustFloat(string value, string msg, out float result)
        {
            bool flag = float.TryParse(value, out result);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }
        /// <summary>
        /// 必须为 DateTime
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="msgCode">验证消息提示错误代码</param>
        /// <param name="result">转换成功后的值</param>
        /// <returns>是否转换成功</returns>
        public bool MustDateTime(string value, string msg, out DateTime result)
        {
            bool flag = DateTime.TryParse(value, out result);
            if (!flag)
            {
                this.AddMessage(msg);
            }
            return flag;
        }

        #endregion
    }
}

